package org.jeecg.modules.param.fault.service.impl;

import cn.hutool.core.bean.copier.BeanCopier;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.constant.BikeConstant;
import org.jeecg.modules.param.fault.entity.BikeFaultSubmit;
import org.jeecg.modules.param.fault.entity.BikeFaultSubmitFile;
import org.jeecg.modules.param.fault.mapper.BikeFaultSubmitFileMapper;
import org.jeecg.modules.param.fault.mapper.BikeFaultSubmitMapper;
import org.jeecg.modules.param.fault.service.IBikeFaultSubmitService;
import org.jeecg.modules.param.fault.vo.SubmitFile;
import org.jeecg.modules.param.member.entity.BikeMemberUser;
import org.jeecg.modules.param.member.mapper.BikeMemberUserMapper;
import org.jeecg.modules.utils.ExcelUtils;
import org.jeecg.modules.utils.RandomCodeUtils;
import org.jeecg.modules.work.repair.entity.BikeWorkRepair;
import org.jeecg.modules.work.repair.mapper.BikeWorkRepairMapper;
import org.jeecg.modules.work.sweep.entity.BikeWorkSweep;
import org.jeecg.modules.work.sweep.mapper.BikeWorkSweepMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;

/**
 * @Description: 上报故障记录
 * @Author: 月月鸟
 * @Date:   2021-05-28
 * @Version: V1.0
 */
@Service
public class BikeFaultSubmitServiceImpl extends ServiceImpl<BikeFaultSubmitMapper, BikeFaultSubmit> implements IBikeFaultSubmitService {
    @Resource
    private BikeFaultSubmitMapper bikeFaultSubmitMapper;
    @Resource
    private BikeFaultSubmitFileMapper bikeFaultSubmitFileMapper;
    @Resource
    private BikeWorkRepairMapper bikeWorkRepairMapper;

    @Resource
    private BikeWorkSweepMapper bikeWorkSweepMapper;
    @Resource
    private BikeMemberUserMapper bikeMemberUserMapper;
    @Value("${application.domain}")
    public String domain;
    @Override
    public List<SubmitFile> pageList(Integer pageNo, Integer pageSize, SubmitFile bikeFaultSubmit) {
        SubmitFile submitFile = new SubmitFile();
        //查询条件
        List<SubmitFile> submitFiles = bikeFaultSubmitMapper.pageList(bikeFaultSubmit);
        QueryWrapper<BikeFaultSubmitFile> wrapper = new QueryWrapper<>();
        List<BikeFaultSubmitFile> bikeFaultSubmitFiles = bikeFaultSubmitFileMapper.selectList(wrapper);
        if (!bikeFaultSubmitFiles.isEmpty()){
            for (SubmitFile sf : submitFiles){
                for (BikeFaultSubmitFile bfs : bikeFaultSubmitFiles){
                    if (sf.getId().equals(bfs.getFaultId())){
                        if (StringUtils.isNotEmpty(bfs.getFileUrl())){
                            if (StringUtils.isNotEmpty(sf.getFileUrl())){
                                sf.setFileUrl(sf.getFileUrl()+","+domain+bfs.getFileUrl());
                            }else {
                                sf.setFileUrl(domain+bfs.getFileUrl());
                            }
                        }
                    }
                }
            }
        }
        return submitFiles;
    }

    @Override
    public Result<?> goWorkOrder(SubmitFile submitFile) {
        BikeWorkRepair bikeWorkRepair = new BikeWorkRepair();
        if (!submitFile.getFaultType().equals("6")){
            //单号
            bikeWorkRepair.setCode(RandomCodeUtils.getRandomCode(BikeConstant.work_repair_open));
            bikeWorkRepair.setStationCode(submitFile.getStationCode());
            bikeWorkRepair.setStationName(submitFile.getStationName());
            //设备编号
            bikeWorkRepair.setDeviceCode(submitFile.getPileCode());
            bikeWorkRepair.setAddress(submitFile.getStationAddress());
            //故障类型(1还不了车2租不了车3网络故障4车辆损坏5车锁故障6需要保洁7其他问题)
            bikeWorkRepair.setRepairItem(submitFile.getFaultType());
            //是否必须上传照片 1是 0否
            bikeWorkRepair.setIsPicture(submitFile.getIsPicture());
            //工单来源 (1新增2检测故障3上报故障)
            bikeWorkRepair.setWorkSource("3");
            //来源编码
            bikeWorkRepair.setSourceId(submitFile.getId());
            //负责人
            bikeWorkRepair.setUserId(submitFile.getUserDutyId());
            //截止时间
            bikeWorkRepair.setFinishTime(submitFile.getFinishTime());
            //是否派单 1是 0否
            bikeWorkRepair.setIsDispatch(submitFile.getIsDispatch());
            if (bikeWorkRepair.getIsDispatch().equals(BikeConstant.y_n_yes)){
                bikeWorkRepair.setDispatchTime(new Date());
            }
            //紧急程度 2 一般 1加急
            bikeWorkRepair.setUrgentType(submitFile.getUrgentType());
            //备注
            bikeWorkRepair.setMemo(submitFile.getMemo());
            bikeWorkRepairMapper.insert(bikeWorkRepair);

        }else {
            BikeWorkSweep bikeWorkSweep = new BikeWorkSweep();
            QueryWrapper<BikeWorkSweep> wrapper = new QueryWrapper<>();
            wrapper.eq("device_code",submitFile.getPileCode());
            wrapper.eq("is_solve", BikeConstant.y_n_no);
            BikeWorkSweep workSweep = bikeWorkSweepMapper.selectOne(wrapper);
            if (workSweep!=null){
                return Result.error("设备存在保洁工单未完成");
            }
            //单号
            bikeWorkSweep.setCode(RandomCodeUtils.getRandomCode(BikeConstant.work_sweep_open));
            //网点code
            bikeWorkSweep.setStationCode(submitFile.getStationCode());
            //网点名
            bikeWorkSweep.setStationName(submitFile.getStationName());
            //桩code
            bikeWorkSweep.setDeviceCode(submitFile.getPileCode());
            //网点地址
            bikeWorkSweep.setAddress(submitFile.getStationAddress());
            //故障类型(1还不了车2租不了车3网络故障4车辆损坏5车锁故障6需要保洁7其他问题)
            bikeWorkSweep.setRepairItem(BikeConstant.y_n_no);
            //是否必须上传照片 1是 0否
            bikeWorkSweep.setIsPicture(submitFile.getIsPicture());
            //工单来源 (1新增2检测故障3上报故障)
            bikeWorkSweep.setWorkSource("3");
            //来源编码
            bikeWorkSweep.setSourceId(submitFile.getId());
            //负责人
            bikeWorkSweep.setUserId(submitFile.getUserDutyId());
            //截止时间
            bikeWorkSweep.setFinishTime(submitFile.getFinishTime());
            //是否派单 1是 0否
            bikeWorkSweep.setIsDispatch(submitFile.getIsDispatch());
            if (bikeWorkSweep.getIsDispatch().equals(BikeConstant.y_n_yes)){
                bikeWorkSweep.setDispatchTime(new Date());
            }
            //紧急程度 2 一般 1加急
            bikeWorkSweep.setUrgentType(submitFile.getUrgentType());
            //备注
            bikeWorkSweep.setMemo(submitFile.getMemo());
            bikeWorkSweepMapper.insert(bikeWorkSweep);

        }
        //添加转工单时间和状态
        BikeFaultSubmit bikeFaultSubmit = new BikeFaultSubmit();
        bikeFaultSubmit.setId(submitFile.getId());
        bikeFaultSubmit.setDispatchTime(new Date());
        bikeFaultSubmit.setIsDispatch(BikeConstant.y_n_yes);
        bikeFaultSubmitMapper.updateById(bikeFaultSubmit);
        return Result.OK("已转工单");
    }


    @Override
    public SubmitFile pageListById(String id) {
        SubmitFile submitFile = bikeFaultSubmitMapper.pageListById(id);
        QueryWrapper<BikeFaultSubmitFile> wrapper = new QueryWrapper<>();
        List<BikeFaultSubmitFile> bikeFaultSubmitFiles = bikeFaultSubmitFileMapper.selectList(wrapper);
        for (BikeFaultSubmitFile bfs : bikeFaultSubmitFiles){
            if (submitFile.getId().equals(bfs.getFaultId())){
                if (submitFile.getFileUrl()!=null){
                    submitFile.setFileUrl(submitFile.getFileUrl()+","+domain+bfs.getFileUrl());
                }else {
                    submitFile.setFileUrl(domain+bfs.getFileUrl());
                }
            }
        }
        return submitFile;
    }

    @Override
    public ModelAndView exportXls(HttpServletRequest request,  SubmitFile bikeFaultSubmit) {
        String stationId = bikeFaultSubmit.getStationId();
        SubmitFile submitFile = new SubmitFile();

        if (bikeFaultSubmit.getCode()!=null){
            submitFile.setCode(bikeFaultSubmit.getCode());
        }
        if (bikeFaultSubmit.getCode()!=null){
            submitFile.setUserId(bikeFaultSubmit.getUserId());
        }
        List<SubmitFile> submitFiles = bikeFaultSubmitMapper.pageList(submitFile);
        QueryWrapper<BikeFaultSubmitFile> wrapper = new QueryWrapper<>();
        List<BikeFaultSubmitFile> bikeFaultSubmitFiles = bikeFaultSubmitFileMapper.selectList(wrapper);
        for (SubmitFile sf : submitFiles){
            for (BikeFaultSubmitFile bfs : bikeFaultSubmitFiles){
                if (sf.getId().equals(bfs.getFaultId())){
                    if (sf.getFileUrl()!=null){
                        sf.setFileUrl(sf.getFileUrl()+","+bfs.getFileUrl());
                    }else {
                        sf.setFileUrl(bfs.getFileUrl());
                    }
                }
            }
        }
        return ExcelUtils.exportXls(request,submitFiles,SubmitFile.class,"上报故障记录");
    }
    //上报故障转工单校验
    @Override
    public Result<?> goCheckWorkOrder(SubmitFile submitFile) {
        if (StringUtils.isNotEmpty(submitFile.getId())){
            BikeFaultSubmit bikeFaultSubmit = bikeFaultSubmitMapper.selectById(submitFile.getId());
            if (bikeFaultSubmit!=null){
                if (!bikeFaultSubmit.getFaultType().equals("6")){
                    QueryWrapper<BikeWorkRepair> wrapper = new QueryWrapper<>();
                    wrapper.eq("source_id",submitFile.getId());
                    List<BikeWorkRepair> bikeWorkRepairs = bikeWorkRepairMapper.selectList(wrapper);
                    if (bikeWorkRepairs.size()!=0){
                        return Result.error("请勿重复转工单");
                    }
                }else {
                    QueryWrapper<BikeWorkSweep> wrapper = new QueryWrapper<>();
                    wrapper.eq("source_id",submitFile.getId());
                    List<BikeWorkSweep> bikeWorkSweeps = bikeWorkSweepMapper.selectList(wrapper);
                    if (bikeWorkSweeps.size()!=0){
                        return Result.error("请勿重复转工单");
                    }
                }
            }
            return Result.OK();
        }else {
            return Result.error("请勾选转工单记录");
        }
    }


}
